package ace.module.tenant.core.impl.service.impl;

import ace.cmp.core.model.PageDto;
import ace.cmp.core.model.PageResultDto;
import ace.cmp.spring.data.jdbc.querydsl.core.converter.PageConverter;
import ace.module.common.api.model.input.PageInput;
import ace.module.account.api.IamAccountApi;
import ace.module.account.api.model.dto.IamAccountDetailDto;
import ace.module.account.api.model.input.AppOutInfoInput;
import ace.module.account.api.model.input.IamAccountFilterInput;
import ace.module.account.api.model.input.IamAccountSearchOutInput;
import ace.module.account.api.model.input.RegisterQuickByUsernameOutInput;
import ace.module.account.api.model.input.RegisterQuickCommonOutInput;
import ace.module.tenant.api.constant.TenantConstant;
import ace.module.tenant.api.model.input.TenTenantAccountSearchInput;
import ace.module.tenant.core.impl.service.TenantAccountService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

@Slf4j
@AllArgsConstructor
@Component
public class TenantAccountServiceImpl implements TenantAccountService {

  private final IamAccountApi iamAccountApi;
  private final PageConverter pageConverter;

  @Override
  public Page<IamAccountDetailDto> search(TenTenantAccountSearchInput input,
      Pageable pageable) {

    IamAccountSearchOutInput searchInput =
        IamAccountSearchOutInput.builder()
            .appOutInfo(
                AppOutInfoInput.builder()
                    .outType(TenantConstant.TENANT_IAM_APP_OUT_TYPE)
                    .outId(input.getTenantId().toString())
                    .build())
            .filter(
                IamAccountFilterInput.builder()
                    .accountId(input.getAccountId())
                    .nickName(input.getNickName())
                    .username(input.getUsername())
                    .build())
            .build();

    PageDto pageDto = pageConverter.toPageDto(pageable);

    PageInput<IamAccountSearchOutInput> pageInput = PageInput.<IamAccountSearchOutInput>builder()
        .page(pageDto)
        .data(searchInput)
        .build();

    PageResultDto<IamAccountDetailDto> pageResultDto =
        iamAccountApi.searchForDetailByOutInfo(pageInput).check();

    return pageConverter.toPage(pageResultDto, pageable);
  }

  @Override
  public Long add(TenTenantAccountSearchInput input) {

    RegisterQuickByUsernameOutInput registerQuickByUsernameOutInput =
        RegisterQuickByUsernameOutInput.builder()
            .username(input.getUsername())
            .outInfo(
                AppOutInfoInput.of(
                    TenantConstant.TENANT_IAM_APP_OUT_TYPE, input.getTenantId().toString()))
            .common(RegisterQuickCommonOutInput.builder().nickName(input.getNickName()).build())
            .build();

    return iamAccountApi
        .registerQuickByUsernameAndAppOutInfo(registerQuickByUsernameOutInput)
        .check();
  }
}
